#!/usr/bin/env python -OO
# -*- coding: utf-8 -*-

# Copyright © 2008,2009 Trust Digital Technology Laboratory, Shanghai Jiao Tong University.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author:
#   XING Dong sheng <homer.xing@gmail.com>

from __future__ import with_statement

__all__ = ['needinteractive']

import os, sys
import softwaredependent as D
from softwareindependent import getArch, installed, filecontain, fileinsert, fileappend, TempOwn, notify
from installvm import installvm

# 新版本的firestarter已经自行去除了 $LANG BUG
#def fixfirestarter():
#	fn = '/etc/firestarter/firestarter.sh'
#	D.execute("sudo touch "+fn)
#	with TempOwn(fn) as o:
#		if not filecontain(fn, 'LANG=en_US #ubuntueasysetuper\n'):
#			notify('注意！',"不要改动Firestarter的任何选项，一路点\"前进\"，最后一步点\"保存\"。")
#			os.system("sudo firestarter >/dev/null 2>/dev/null &")
#			if D.execute("sleep 2; sudo killall firestarter"): #等待firestarter.sh生成，再关闭之
#				with TempOwn(fn) as o:
#					fileinsert(fn, 2, "LANG=en_US #ubuntueasysetuper\n")

def firestarter():
	D.install("firestarter")

def fixsunjdketcenv():
	if not filecontain("/etc/environment", "java-6-sun"):
		with TempOwn("/etc/environment") as o:
			fileappend('/etc/environment', """JAVA_HOME=/usr/lib/jvm/java-6-sun
JAVA_BIN=/usr/lib/jvm/java-6-sun/bin
CLASSPATH=.:/usr/lib/jvm/java-6-sun/lib/dt.jar:/usr/lib/jvm/java-6-sun/lib/tools.jar
""")

def fixsunjdkchoosejvm():
	#默认用SUN的Java
	D.execute("sudo update-java-alternatives -s java-6-sun >/dev/null 2>/dev/null")
	fn = "/etc/jvm"
	with TempOwn(fn) as o:
		with open(fn, "w") as f:
			f.write("/usr/lib/jvm/java-6-sun\n")

def fixsunjdkfont():
	#修正SUN-JDK中文字体显示不对的BUG
	#FIXME 修正/usr/lib/jvm/jdk1.6.0_13/
	if not os.path.exists('/usr/lib/jvm/java-6-sun/jre/lib/fonts/fallback/wqy-zenhei.ttf'):
		if D.install("ttf-wqy-zenhei") and D.execute("sudo mkdir /usr/lib/jvm/java-6-sun/jre/lib/fonts/fallback/"):
			D.execute("sudo cp /usr/share/fonts/truetype/wqy/wqy-zenhei.* /usr/lib/jvm/java-6-sun/jre/lib/fonts/fallback/")
	D.execute("sudo ln -sf /usr/share/fonts/truetype/arphic/uming.ttc /usr/share/fonts/truetype/arphic/uming.ttf")

#OpenJDK的中文字体总是显示不对。所以不再安装OpenJDK
def sunjdk():
	#不能屏蔽输出，因为要同意SUN许可证
	#FIXME: 不是图形窗口，如何自动化？
	if not installed('sun-java6-jdk'):
		notify('您必须同意许可证，才能安装 SUN Java6 JDK。', '窗口显示 "Operating System Distributor License for Java" 时，按一下 Tab 键选中 "&lt;OK&gt;"，再按空格。')
		D.install("sun-java6-jdk")
	fixsunjdketcenv()
	fixsunjdkchoosejvm()
	fixsunjdkfont()

def eioffice():
	#用国产免费永中Office替代OpenOffice。
	f1=D.download("eioffice1")
	f2=D.download("eioffice2")
	f3=D.download("eioffice3")
	f4=D.download("eioffice4")
	f5=D.download("eioffice5")
	#FIXME 没有自动化！
	if D.execute("cd "+D.LOCAL+";" \
		+("tar zxf %s; sudo EIOffice_Personal/setup"%f1)):
		notify("Ubuntu Easy Setuper 说：",
"接下来安装剪贴画、帮助系统、公式编辑器、模版。它们的安装窗口是完全相同的, 不是同一个程序安装四遍 =,= 不是BUG。")
		if D.execute("cd "+D.LOCAL+";" +("tar zxf %s; sudo ./setup; "%f2)):
			if D.execute("cd "+D.LOCAL+";" +("tar zxf %s; sudo ./setup; "%f3)):
				if D.execute("cd "+D.LOCAL+";" +("tar zxf %s; sudo ./setup; "%f4)):
					D.execute("cd "+D.LOCAL+";" +("tar zxf %s; sudo ./setup"%f5))

def installsslcert():
	#升级'ssl-cert'时可能需要用户交互
	if not installed('ssl-cert'):
		D.install("ssl-cert")

netbeansloc='/usr/local/netbeans-6.7/'
netbeansstart=netbeansloc+'bin/netbeans'

def fixnetbeansj2se():
	#修正部分J2ME图形化程序不能启动的BUG
	if not filecontain(netbeansstart, 'LANG=C #ubuntueasysetuper\n'):
		with TempOwn (netbeansstart) as o:
			fileinsert(netbeansstart, 1, "LANG=C #ubuntueasysetuper\n")

j2sei386='/usr/lib/jvm/jdk1.6.0_13'

def installi386jdk():
		if os.path.exists(j2sei386)==False:
			f=D.download('jdk1.6.0_i386')
			D.execute('cd /usr/lib/jvm/; sudo tar zxf %s'%f)

def fixnetbeansj2me():
	emu=netbeansloc+'mobility8/WTK2.5.2/bin/emulator'
	toadd='javapathtowtk=%s/bin/ #ubuntueasysetuper\n'%j2sei386
	if getArch()==64 and not filecontain(emu, toadd):
		installi386jdk()
		with TempOwn(emu) as o:
			fileinsert(emu, 4, toadd)

def netbeans():
	if not os.path.exists(netbeansstart):
		f=D.download("netbeans")
		D.execute("chmod a+x %s; sudo %s" % (f, f))
	fixnetbeansj2se()
	fixnetbeansj2me()

def kernel_2_6_30():
	if not os.path.exists('/boot/vmlinuz-2.6.30-020630-generic'):
		k0=D.download('kernel.0')
		k1=D.download('kernel.1.')
		k2=D.download('kernel.2.')
		if D.execute("sudo dpkg -i %s"%k0):
			if D.execute("sudo dpkg -i %s"%k1):
				D.execute("sudo dpkg -i %s"%k2)

def downthemall():
	CDLOCAL = "cd %s && "%D.LOCAL
	D.download("downthemall")
	xpi = D.softwarelist["downthemall"]
	D.execute(CDLOCAL+" firefox %s &"%xpi)

def needinteractive():
	installsslcert()
	if 'B' in D.choice:
		kernel_2_6_30()
		firestarter()
	if 'C' in D.choice:
		downthemall()
		eioffice()
	if 'P' in D.choice:
		sunjdk()
		netbeans()
	if 'V' in D.choice:
		installvm()

if __name__ == "__main__":
	needinteractive()
